Ext.ns('CDRUtils');
Ext.define('CDR.utils.CDRUtils', {
 	statics:{
		TariffType: {
			TARIFF: 'TARIFF',
			GROUP: 'GROUP',
			AREACODE: 'AREACODE'
		},
	 	getTariffTree: function() {
	 		return Ext.getCmp('x-tariff-setting-treepanel');
	 	},
	 	
	 	getAllGroups: function(excludeNode){
	 		var tree = CDRUtils.getTariffTree();
	 		var groups = tree.getRootNode().childNodes[0].childNodes;
	 		var data = [];
	 		
	 		Ext.iterate(groups, function(g){
	 			if(excludeNode){
					if(excludeNode.data.obId != g.data.obId){
						data.push({id: g.data.obId, text: g.data.text});
					}
	 			} else {
	 				data.push({id: g.data.obId, text: g.data.text});
	 			}
	 		});
	 		return data;
	 	},
	 	
	 	getAllAreaCodes: function(id){
	 		var tree = CDRUtils.getTariffTree();
	 		var groups = tree.getRootNode().childNodes[0].childNodes;
	 		var childNodes = [];
	 		var data = [];
	 		Ext.iterate(groups, function(g){
	 			if(id === g.data.obId){
	 				childNodes = g.childNodes;
	 				return false;
	 			}
	 		});
	 		Ext.iterate(childNodes, function(n){
	 			data.push({id: n.data.obId, text: n.data.text});
	 		});
	 		return data;
	 	},
	 	
	 	reloadTariffTree: function() {
	 		var tree = CDRUtils.getTariffTree();
	 		var treeId = tree.getRootNode().childNodes[0].data.id;
	 		tree.getRootNode().removeAll();
			tree.getStore().load({params: {
				treeId: treeId
			}});
	 	},
	 	
	 	getTariffSelectedNodes: function() {
	 		var tree = CDRUtils.getTariffTree();
	 		if(tree){
	 			return tree.getSelectionModel().getSelection();
	 		}
	 		return null;
	 	},
	 	
	 	getTariffSelectedNode: function() {
	 		var tree = CDRUtils.getTariffTree();
	 		if(tree){
	 			return tree.getSelectionModel().getSelection()[0];
	 		}
	 		return null;
	 	},
	 	
	 	saveTariffNode: function(url, params, callback){
	 		Ext.Ajax.request({
				url: url,
				params: params,
				success: function(response){
					var data = Ext.decode(response.responseText);
					if(data.success) {
						if(typeof callback === 'function'){
							callback(data);
						}
					}
				},
				failure:function(){
					alert('save tariff failed...');
				}
			});
	 	},
	 	
	 	loadTariffNode: function(url, params, callback){
	 		Ext.Ajax.request({
				url: url,
				method: 'GET',
				params: params,
				success: function(response){
					var data = Ext.decode(response.responseText);
					if(data.success) {
						if(typeof callback === 'function'){
							callback(data);
						}
					}
				},
				failure:function(){
					alert('load tariff failed...');
				}
			});
	 	},
	 	
	 	createTariffNode: function() {
	 		 CDRUtils.saveTariffNode('saveTariffNode',{
	 		 	name: 'Tariff Table',
	 		 	type: CDRUtils.TariffType.TARIFF,
	 		 }, function(data) {
	 		 	var tree = CDRUtils.getTariffTree();
	 		 	var newNode = {
	 		 		text: data.result.name,
		   			leaf: true,
		   			id: data.result.id,
		   			type: CDRUtils.TariffType.TARIFF
	 		 	};
		   		var node = tree.getRootNode().appendChild(newNode);
		   		CDRUtils.getTariffTree().doComponentLayout();
	 		 });
	 	},
	 	
	 	createGroupNode: function() {
			var node = CDRUtils.getTariffSelectedNode();
			if(node){
				CDRUtils.saveTariffNode('saveGroupNode',{
					type: CDRUtils.TariffType.GROUP,
		 			name: 'Group Type ' + node.childNodes.length,
		 			tariffId: node.data.id,
		 		}, function(data) {
		 			var newNode = {
		 		 		text: data.result.name,
			   			leaf: true,
			   			type: CDRUtils.TariffType.GROUP,
			   			obId: data.result.id
		 		 	};
			   		newNode = node.appendChild(newNode);
			   		CDRUtils.getTariffTree().doComponentLayout();
		 		 });
			}
	 	},
	 	
	 	createAreaNode: function() {
			var node = CDRUtils.getTariffSelectedNode();
			if(node){
				CDRUtils.saveTariffNode('saveAreaNode',{
					type: CDRUtils.TariffType.AREACODE,
					areaCode: (new Date()).getTime(),
		 			name: 'Area Code ' + node.childNodes.length,
		 			tariffGroupId: node.data.obId,
		 		}, function(data) {
		 			var newNode = {
		 		 		text: data.result.areaCode + ' ' + data.result.name ,
			   			leaf: true,
			   			type: CDRUtils.TariffType.AREACODE,
			   			obId: data.result.id
		 		 	};
		 		 	node.data['leaf'] = false;
		 		 	node.data['expanded'] = true;
			   		newNode = node.appendChild(newNode);
			   		CDRUtils.getTariffTree().doComponentLayout();
		 		 });
			}
	 	},
	 	
	 	cloneAreaNode: function(){
	 		var node = CDRUtils.getTariffSelectedNode();
			if(node){
				Ext.Ajax.request({
					url: 'cloneAreaNode',
					params: {areaNodeId: node.data.obId},
					success: function(response){
						var data = Ext.decode(response.responseText);
						if(data.success) {
							var newNode = {
				 		 		text: data.result.areaCode + ' ' + data.result.name ,
					   			leaf: true,
					   			type: CDRUtils.TariffType.AREACODE,
					   			obId: data.result.id
				 		 	};
					   		newNode = node.parentNode.appendChild(newNode);
					   		CDRUtils.getTariffTree().doComponentLayout();
						}
					},
					failure:function(){
						alert('clone tariff failed...');
					}
				});
			}
	 	},
	 	
	 	listTariffNode: function(comp){
	 		Ext.Ajax.request({
				url: 'listTariffs',
				success: function(response){
					var data = Ext.decode(response.responseText);
					if(data && comp) {
						var menuItems = [];
						Ext.iterate(data, function(r){
							menuItems.push({
								xtype: 'menucheckitem',
								text: r.text,
								id: r.id
							});
						});
						comp.addDocked({
							xtype: 'toolbar',
							dock: 'top',
							items: [{
								xtype: 'cycle',
								showText: true,
								menu: {
									xtype: 'menu',
									items: menuItems
								}
							}]
						});	
						
						CDRUtils.initTariffTree(menuItems[0].id);
					}
				}
			});
	 	},
	 	
	 	initTariffTree: function(id) {
	 		var treeContainer = Ext.getCmp('x-tariff-setting-tree');
	 		treeContainer.removeAll();
	 		var editingPlugin = Ext.create('Ext.ux.plugin.TreeEditPlugin', {
				 clicksToEdit: 2,
	             listeners: {
	             	beforeedit: function(e){
	             		
	             	},
	             	edit: function(editor, obj){
	             		if(obj.originalValue !== obj.value) {
	             			 CDRUtils.saveTariffNode('saveTariffNode', {
					 		 	name: obj.value,
					 		 	id: obj.record.data.id
					 		 }, function() {
					 		 	obj.record.commit();
					 		 });
	             		}
	             	}
	             }
			});
			var treeStore = Ext.create('CDR.store.TariffTreeStore');
			treeStore.load({
				params: {
					treeId: id
				}
			});
	 		treeContainer.add({
				xtype: 'treepanel',
				id: 'x-tariff-setting-treepanel',
				border: false,
				store: treeStore,
				rootVisible: false,
				hideHeaders: true,
				editingPlugin: editingPlugin,
				plugins: [editingPlugin],
				columns: [{
					xtype: 'treecolumn',
					dataIndex: 'text',
					flex:1,
					menuDisabled:true,
					renderer: function(val, style, record){
						var expandId = CDRUtils.localStore.findExact('expandId', record.data.obId);
						/*if(expandId > 0){
							record.data.expanded = true;
							console.log(record);
						}*/
						if(val.indexOf("@Template") >= 0){
							return '<span style="color:red;">' + val + '</span>';
						}
						return val;
					},
					editor:{
		    			xtype:'textfield',
		    			fieldLabel:' ',
		    			labelSeparator:'',
		    			labelWidth: 0,
		    			width: 300
		    		}
				}]
			});
	 	},
	 	
	 	deleteTariffNode: function(url, params, callback){
	 		Ext.Ajax.request({
				url: url,
				params: params,
				success: function(response){
					var data = Ext.decode(response.responseText);
					if(data.success) {
						if(typeof callback === 'function'){
							callback(data);
						}
					}
				},
				failure:function(){
					alert('delete tariff failed...');
				}
			});
	 	},
	 	
	 	getTreePath: function(node){
	 		var rs = "";
	 		if(node && node.parentNode && node.parentNode.data.id != 'root'){
	 			rs = CDRUtils.getTreePath(node.parentNode);
	 		}  
	 		return rs += " / " + node.data.text;
	 	}
	 	
	 }
}, function() {
	//create alias
	CDRUtils =  CDR.utils.CDRUtils;
});

